package com.joshua.netdemo.MVPDemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.joshua.netdemo.R;

public class MvpAty extends AppCompatActivity implements IUserView, View.OnClickListener {
    private EditText edtID, edtFirst, edtLast;
    private Button btnSave, btnRead;
    private UserPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mvp_aty);
        edtID = (EditText) findViewById(R.id.id);
        edtFirst = (EditText) findViewById(R.id.first_name);
        edtLast = (EditText) findViewById(R.id.last_name);
        btnSave = (Button) findViewById(R.id.save);
        btnRead = (Button) findViewById(R.id.read);

        btnSave.setOnClickListener(this);
        btnRead.setOnClickListener(this);

        mPresenter = new UserPresenter(this);
    }

    @Override
    public int getID() {
        return Integer.valueOf(edtID.getText().toString().trim());
    }

    @Override
    public String getFirstName() {
        return edtFirst.getText().toString().trim();
    }

    @Override
    public String getLastName() {
        return edtLast.getText().toString().trim();
    }

    @Override
    public void setFirstName(String firstName) {
        edtFirst.setText(firstName);
    }

    @Override
    public void setLastName(String lastName) {
        edtLast.setText(lastName);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.save:
                mPresenter.saveUser(getID(), getFirstName(), getLastName());
                break;
            case R.id.read:
                mPresenter.loadUser(getID());
                break;
        }
    }
}
